﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CADDemo2025
{
    public class Command0203
    {
        [CommandMethod("Demo02031")]
        public void Demo02031()
        {
            if (!UmUserMethod.GetEntity(out Curve curve)) return;
            Jig jig = new Jig(Point3d.Origin, (p, e1, e2) =>
            {
                Point3d closestPo = curve.GetClosestPointTo(p, false);
                Line line1 = new Line(closestPo, p);
                Line line2 = new Line(closestPo, closestPo + line1.Delta * 0.1);
                line2.TransformBy(Matrix3d.Rotation(Math.PI / 6, Vector3d.ZAxis, closestPo));
                Line line3 = new Line(closestPo, closestPo + line1.Delta * 0.1);
                line3.TransformBy(Matrix3d.Rotation(-Math.PI / 6, Vector3d.ZAxis, closestPo));
                e1.AddRange(new List<Entity>() { line1, line2, line3 });
                e2.AddRange(new List<Entity>() { line1, line2, line3 });
            },"选择终点");
        }
        [CommandMethod("Demo02032")]
        public void Demo02032()
        {
            if (!UmUserMethod.SelectEntities(out List<Curve>  curves)) return;
            Jig jig = new Jig(Point3d.Origin, (p, e1, e2) =>
            {
                Point3d closestPo = curves[0].GetClosestPointTo(p, false);
                foreach (var curve in curves)
                {
                    Point3d po = curve.GetClosestPointTo(p, false);
                    if(po.DistanceTo(p)<closestPo.DistanceTo(p))
                    {
                        closestPo = po;
                    }
                }
                Line line1 = new Line(closestPo, p);
                Line line2 = new Line(closestPo, closestPo + line1.Delta.GetNormal() * 10);
                line2.TransformBy(Matrix3d.Rotation(Math.PI / 6, Vector3d.ZAxis, closestPo));
                Line line3 = new Line(closestPo, closestPo + line1.Delta.GetNormal() * 10);
                line3.TransformBy(Matrix3d.Rotation(-Math.PI / 6, Vector3d.ZAxis, closestPo));
                e1.AddRange(new List<Entity>() {  line2, line3 });
                e2.AddRange(new List<Entity>() { line1, line2, line3 });
            }, "选择终点");

        }
    }
}
